package cn.stylefeng.guns.modular.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.util.StringUtil;
import cn.stylefeng.guns.modular.system.entity.Exam;
import cn.stylefeng.guns.modular.system.entity.ExamContent;
import cn.stylefeng.guns.modular.system.entity.ExamPlaceClass;
import cn.stylefeng.guns.modular.system.entity.ExamUser;
import cn.stylefeng.guns.modular.system.mapper.ExamMapper;
import cn.stylefeng.guns.modular.system.model.ExamDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试表 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-10
 */
@Service
public class ExamService extends ServiceImpl<ExamMapper,Exam> {
    @Autowired
    private ExamUserService examUserService;
    @Autowired
    private ExamContentService examContentService;
    @Autowired
    private SysStudentService studentService;


    public IPage<ExamDto> examList(String examTitle, Long examId) {
        return this.baseMapper.examList(LayuiPageFactory.defaultPage(),
                ShiroKit.isAdmin()?null: ShiroKit.getUserNotNull().getId(),examTitle,examId);
    }

    public void examSave(Exam exam) {
        if(ToolUtil.isEmpty(exam.getExamId()) && ToolUtil.isEmpty(exam.getCreateUser())){
            exam.setCreateUser(ShiroKit.getUserNotNull().getId());
        }
        exam.setCreateTime(LocalDateTime.now());
        exam.setEffective(1);
        if(ToolUtil.isNotEmpty(exam.getExamContent())){
            List<ExamContent> examContentList = JSONArray.parseArray(exam.getExamContent(), ExamContent.class);
            examContentList.forEach(examContent -> {
                BeanUtil.copyProperties(exam,examContent);
                examContentService.saveOrUpdate(examContent);
            });
        }
        this.saveOrUpdate(exam);
    }

    public List<ExamDto> examList(String createUsers, String examMonthIds, Integer soldOut, String examIds, Long userId) {
        List<ExamDto> examDtos = this.baseMapper.examList2(StringUtil.strSplit(createUsers),
                StringUtil.strSplit(examMonthIds) , soldOut, StringUtil.strSplit(examIds));
        examDtos.forEach(dto->{
            dto.setExamContentList(examContentService.list(new QueryWrapper<ExamContent>().eq("exam_id",dto.getExamId())));
            if(ToolUtil.isNotEmpty(userId)){
                dto.setExamUser(examUserService.getOne(
                        new QueryWrapper<ExamUser>()
                                .eq("exam_id", dto.getExamId()).eq("user_id", userId)));
                dto.setStudent(studentService.getStudent(userId));
            }
        });
        return examDtos;
    }

    public IPage<Map<String,Object>> paymentRecord (String name){
        return this.baseMapper.paymentRecord(LayuiPageFactory.defaultPage(),
                ShiroKit.isAdmin()?null:ShiroKit.getUserNotNull().getId(),name);
    }


}
